package com.snail.snailvr.views;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.MenuItem;

import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import com.journeyapps.barcodescanner.CompoundBarcodeView;
import com.snail.snailvr.R;
import com.snail.snailvr.utils.CustomCaptureManager;
import com.snail.snailvr.utils.IntentUtils;
import com.snail.snailvr.utils.LogUtils;
import com.snail.snailvr.utils.ToastUtils;
import com.snail.snailvr.utils.Util;
import com.snail.snailvr.widget.CustomViewfinderView;
import com.umeng.analytics.MobclickAgent;

import java.util.concurrent.TimeUnit;

import butterknife.Bind;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;

/**
 * Custom Scannner Activity extending from Activity to display a custom layout form scanner view.
 */
public class ScannerActivity extends BaseToolbarActivity implements CustomCaptureManager.ScanCompleteListener {
    private static final long DELAY_FINISH_TIME = 3;
    @Bind(R.id.toolbar)
    public Toolbar mToolbar;
    private CustomCaptureManager capture;
    @Bind(R.id.zxing_barcode_scanner)
    CompoundBarcodeView barcodeScannerView;
    @Bind(R.id.zxing_viewfinder_view)
    CustomViewfinderView mCustomViewfinderView;

    @Override
    protected void handleIntent() {

    }

    @Override
    protected void initView() {
        setupToolbar(mToolbar, getString(R.string.scan_qr_code), -1);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_scanner;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mCustomViewfinderView.setBorderColor(getResources().getColor(R.color.positive_normal_color));
        capture = new CustomCaptureManager(this, barcodeScannerView);

        capture.setScanCompleteListener(this);
        capture.initializeFromIntent(getIntent(), savedInstanceState);
        capture.decode();
    }

    @Override
    protected void onResume() {
        super.onResume();
        capture.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        capture.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        capture.onDestroy();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        capture.onSaveInstanceState(outState);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return barcodeScannerView.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
    }

    @Override
    protected boolean onToolbarMenuItemClick(MenuItem item) {
        return false;
    }

    @Override
    public void onScanComplete(Intent intent) {
        IntentResult result = IntentIntegrator.parseActivityResult(IntentIntegrator.REQUEST_CODE, RESULT_OK, intent);
        if (result != null) {
            if (result.getContents() == null) {
                LogUtils.d("Cancelled scan");
            } else {
                String qr_url = result.getContents();
                if (!TextUtils.isEmpty(qr_url) && Util.isLiveUrl(qr_url)) {
                    IntentUtils.playVideo(ScannerActivity.this, qr_url, "");
                    finish();
                } else {
                    mCustomViewfinderView.setBorderColor(getResources().getColor(R.color.invalid_live_url_color));
                    ToastUtils.makeToastColorShow(ScannerActivity.this, findViewById(R.id.rootView), getString(R.string.hint_live_url));

                    Observable.timer(DELAY_FINISH_TIME, TimeUnit.SECONDS).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Long>() {
                        @Override
                        public void call(Long aLong) {
                            capture.decode();
                            capture.onResume();
                            mCustomViewfinderView.setBorderColor(getResources().getColor(R.color.zxing_custom_viewfinder_laser));
                        }
                    });
                }
            }
        }
    }

}
